package acc

import org.apache.spark.broadcast.Broadcast
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.mutable

object SparkBc_Test {
  def main(args: Array[String]): Unit = {
    val sparkConf=new SparkConf().setMaster("local[*]").setAppName("combineByKey")
    val sc=new SparkContext(sparkConf)
    val rdd1=sc.makeRDD(List(("a",1),("b",2),("c",3)))
    val map: mutable.Map[String, Int] = mutable.Map(("a", 2), ("b", 2), ("c", 3))
//    rdd1.join(rdd2).collect().foreach(println)
// 封装广播变量
val bc: Broadcast[mutable.Map[String, Int]] = sc.broadcast(map)

    rdd1.map {
      case (w, c) => {
        // 方法广播变量
        val l: Int = bc.value.getOrElse(w,0)
        (w, (c, l))
      }
    }.collect().foreach(println)

    sc.stop()

  }

}
